package com.benmei.weike.web;

import com.benmei.weike.common.Lang;
import com.benmei.weike.dto.PageRequest;
import com.benmei.weike.dto.PageResponse;
import com.benmei.weike.service.StudentService;
import com.benmei.weike.service.common.MemcachedService;
import com.benmei.weike.util.JsonUtil;
import com.benmei.weike.web.vo.my.RecordingDetailRequest;
import com.benmei.weike.web.vo.my.RecordingDetailResponse;
import com.benmei.weike.web.vo.my.ReserveVo;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生端 微课接口
 */
@RestController
public class StudentMyController {

    public static final Logger logger = LoggerFactory.getLogger(StudentMyController.class);

    @Autowired
    private MemcachedService memcachedService;
    @Autowired
    private StudentService studentService;

    //log 记录方法入惨的前缀
    private static final String prefix_in = "\nparameters in -->> ";
    //log 记录方法出惨的前缀
    private static final String prefix_out = "\nparameters out -->>";

    // 1v1上课录音（v4.5）
    @RequestMapping(value = "/student/token/my/recording", method = {RequestMethod.POST})
    @ResponseBody
    public RetInfo studentIndexRecomend(@RequestBody PageRequest pageRequest,@RequestHeader(value = "token") String token) {
        try {
            logger.info(prefix_in + JsonUtil.toJson(pageRequest));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = memcachedService.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            logger.info("memb_id:" + tdNtMember.getMemb_id());
            PageResponse obj = studentService.findReserve(pageRequest,tdNtMember.getMemb_id());
            RetInfo retInfo = RetInfo.getSuccessInfo(obj);
            logger.info(prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);
            logger.error(prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    // 1v1上课录音 - 这堂课的录音列表（v4.5）
    @RequestMapping(value = "/student/token/my/recording/detail", method = {RequestMethod.POST})
    @ResponseBody
    public RetInfo studentIndexRecomend(@RequestBody RecordingDetailRequest request, @RequestHeader(value = "token") String token) {
        try {
            logger.info(prefix_in + JsonUtil.toJson(request));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = memcachedService.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            logger.info("memb_id:" + tdNtMember.getMemb_id());
            RecordingDetailResponse  obj= studentService.findRecordingDetail(request,tdNtMember.getMemb_id());
            RetInfo retInfo = RetInfo.getSuccessInfo(obj);
            logger.info(prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);
            logger.error(prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }


}
